'use strict';

import { gameModel } from "../model/game";
import { ServiceBase, Exceptions } from '../kalend';
import { usergamehistoryService } from "./usergamehistory";
import { usertokenService } from "./usertoken";
import { customer_accountService } from "./customer_account";
var moment = require('moment');

class GameService extends ServiceBase {
    constructor() {
        super(gameModel)
    }
    async query(params: any) {
        const offset = params.offset;
        const limit = params.limit;
        const status = params.status;
        const filter: any = {};
        if (status === 0 || status === 1) {
            filter.status = status;
        }

        let result: any = await this.queryPagination(offset, limit, {
            filter: filter,
            populateOptions: {
                path: 'createuser',
                select: 'name'
            }
        });

        result.rows = result.rows.map((item: any) => {
            if (item.status == '0') {
                item.status = '禁用';
            } else if (item.status == '1') {
                item.status = '有效';
            }
            item.createuser = item.createuser ? item.createuser.name : '';
            return item;
        });

        return result;
    }
    async save(doc: any) {
        if (doc.starttime) {
            doc.starttime = new Date(moment(new Date(doc.starttime)).format('YYYY-MM-DD') + '  00:00:00');
        }
        if (doc.endtime) {
            doc.endtime = new Date(moment(doc.endtime).format('YYYY-MM-DD') + '  23:59:59');
        }
        if (doc._id) {
            this.update(doc._id, doc);
        } else {
            this.create(doc);
        }
        return '保存成功';
    }
    async querygame(token: any) {
        try {
            let game = await this.querySingle({});
            let today = moment(new Date()).format('YYYY-MM-DD');

            game.enable = true;
            if (game) {
                if (game.status === 0) {
                    game.msg = "活动已暂停";
                    game.enable = false;
                } else {
                    if (new Date() < game.starttime) {
                        game.msg = "活动尚未开始，敬请期待";
                        game.enable = false;
                        //throw Exceptions.known("活动尚未开始，敬请期待");
                    } else if (new Date() > game.endtime) {
                        game.msg = "活动已结束，你来晚咯";
                        game.enable = false;
                        //throw Exceptions.known("活动已结束，你来晚咯");
                    } else if (new Date().getDate() !== game.time) {
                        game.msg = "抽奖活动每月9日才开放，敬请期待";
                        game.enable = false;
                        //throw Exceptions.known("活动已结束，你来晚咯");
                    } else if (!token) {
                        game.msg = "请先登录，才能参与抽奖活动哦";
                        game.enable = false;
                        //throw Exceptions.notoken();
                    } else {
                        try {
                            let userid = (await usertokenService.getToken(token)).userid;
                            let user = await customer_accountService.querySingle({ _id: userid }, { select: 'ismember' });
                            if (!user.ismember) {
                                game.msg = "请先升级为会员，才能参与抽奖活动哦";
                                game.enable = false;
                            } else {
                                let usergame = await usergamehistoryService.querySingle({ userid: userid, joinDate: today }, { select: 'count', sort: { createTime: -1 } });
                                if (usergame) {
                                    game.count = usergame.count;
                                }
                            }
                        } catch (error) {
                            game.msg = "登录状态已过期，请返回重新登录";
                            game.enable = false;
                        }
                    }
                }
            }
            return game;
        } catch (error) {
            console.log(error);
            console.log("获取活动配置失败");
            //throw Exceptions.known("获取活动配置失败");
        }
    }
}

export const gameService = new GameService();